﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VTLive.Models;

namespace VTLive.Controllers
{
    public class ScheduleController : BaseController
    {
        //
        // GET: /Schedule/
        
        
        public ActionResult Index()
        // Það er ekkert view fyrir tóm schedule
        {
            return View("404");
        }
        
        [HttpGet]
        public ActionResult Add(int id)
        // Bæta við dagskrá á dag
        {
            StationRepository SRep = new StationRepository();
            if (m_iUserID != SRep.GetStation(id).OwnerID) return View("BadAccess");                
            
                Schedule Sch = new Schedule();
                Sch.StationID = id;
                return View(Sch);
        }


        [HttpPost]
        public ActionResult Add(int id, FormCollection coll)
        {

                ScheduleRepository SRep = new ScheduleRepository();
                Schedule Sch = new Schedule();
                Sch.StartTime = DateTime.Parse(Request.Form["StartTime"]);
                Sch.StationID = id;
                SRep.Add(Sch);
                SRep.Save();
                return RedirectToAction("Manage", "Station",  new {id = id });
        }

        [HttpGet]
        public ActionResult Edit(int id)
        //Bæta við dagskrárliðum, henda þeim og samþykkja tillögur
        {
            ////// Authentication ////////////////////////////////////
            StationRepository SRep = new StationRepository();
            ScheduleRepository SchRep = new ScheduleRepository();
            Schedule mySchedule = SchRep.GetSchedule(id);
            Station myStation = SRep.GetStation(mySchedule.StationID);
            if (m_iUserID != myStation.OwnerID) return View("BadAccess");
            //////////////////////////////////////////////////////////
    
            EpisodeRepository ERep = new EpisodeRepository();
            VideoRepository VRep = new VideoRepository();
            ScheduleViewModel mod = new ScheduleViewModel();
            SuggestionRepository SugRep = new SuggestionRepository();
            mod.episode = new Episode();
            mod.episode.ScheduleID = id;
            mod.video = new Video();
            mod.episodes = ERep.GetAllEpisodesOfSchedule(id);
            mod.videos = VRep.GetAllVideosOfSchedule(id);
            mod.suggestions = SugRep.GetSuggestionsForStation(SchRep.GetSchedule(id).StationID);
            return View("AddEpisode", mod);
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection coll)
        {
            VideoRepository Vrep = new VideoRepository();
            EpisodeRepository ERep = new EpisodeRepository();
            ScheduleRepository SchRep = new ScheduleRepository();
            SuggestionRepository SugRep = new SuggestionRepository();
            Video vid = new Video();
            Episode ep = new Episode();
            
            ScheduleViewModel mo = new ScheduleViewModel();
            UpdateModel(mo);

            vid.Description = Request.Form["Description"];
            vid.Link = Request.Form["Link"];
            vid.Name = Request.Form["Name"];

            ep.VideoID = Vrep.InsertReturnID(vid);
            ep.ScheduleID = id;

            // GetNextOrdernum kastar villu ef enginn þáttur er á dagskrá
            try
            {
                ep.OrderNum = ERep.GetNextOrderNum(ep.ScheduleID);
            }
            catch (System.InvalidOperationException)
            {
                ep.OrderNum = 1;
            }
            ERep.Add(ep);
            ERep.Save();
            Vrep.Save();

            mo.episodes = ERep.GetAllEpisodesOfSchedule(id);
            mo.videos = Vrep.GetAllVideosOfSchedule(id);
            mo.video = new Video();
            mo.episode = new Episode();
            mo.episode.ScheduleID = id;
            mo.suggestions = SugRep.GetSuggestionsForStation(SchRep.GetSchedule(id).StationID);
            
            return View("AddEpisode", mo);
        }
        
        public ActionResult Delete(int id)
        // Henda dagskrárlið af dagskrá
        {
            ////// Authentication //////////////////////////////////////
            StationRepository SRep = new StationRepository();
            VideoRepository VRep = new VideoRepository();
            EpisodeRepository ERep = new EpisodeRepository();
            ScheduleRepository SchRep = new ScheduleRepository();
            Video myVideo = VRep.GetVideo(id);
            Episode myEpisode = ERep.GetEpisodeByVideoID(myVideo.ID);
            Schedule mySchedule = SchRep.GetSchedule(myEpisode.ScheduleID);
            Station myStation = SRep.GetStation(mySchedule.StationID);
            if (m_iUserID != myStation.OwnerID) return View("BadAccess");                
            //////////////////////////////////////////////////////////////
            ScheduleViewModel mod = new ScheduleViewModel();
            mod.episode = new Episode();
            mod.video = new Video();
            int myid = new int();
            myid = ERep.GetEpisodeByVideoID(id).ScheduleID;
            mod.episodes = ERep.GetAllEpisodesOfSchedule(myid);
            mod.videos = VRep.GetAllVideosOfSchedule(myid);

            ERep.Delete(ERep.GetEpisodeByVideoID(id));
            ERep.Save();
            VRep.Delete(VRep.GetVideo(id));
            VRep.Save();
            //return View("AddEpisode", mod);
            return RedirectToAction("Edit", new {id = myid});
        }
        [HttpGet]
        public ActionResult Suggest(int id)
        {
            // Senda tillögu að dagskrárlið

            // Authentication hluti
            StationRepository SRep = new StationRepository();
            AssociateRepository ARep = new AssociateRepository();
            Station myStation = SRep.GetStation(id);
            if (! ARep.IsAssociate(m_iUserID, myStation)) return View("BadAccess");                

            //Suggest hluti
            SuggestionRepository Srep = new SuggestionRepository();
            SuggestionViewModel mod = new SuggestionViewModel();
                mod.video = new Video();
                mod.suggestions = Srep.GetSuggestionsForStation(id);
                return View("AddSuggestionView", mod);
        }


        [HttpPost]
        public ActionResult Suggest(int id, FormCollection coll)
        {
            AssociateRepository ARep = new AssociateRepository();
            ScheduleRepository ScRep = new ScheduleRepository();
            SuggestionRepository SRep = new SuggestionRepository();
            VideoRepository Vrep = new VideoRepository();
            SuggestionViewModel mod = new SuggestionViewModel();
            mod.video = new Video();
            UpdateModel(mod);
            mod.video.Name = Request.Form["Name"];
            mod.video.Link = Request.Form["Link"];
            
            Suggestion Suggestion = new Suggestion();

            int StationID = id;
            Suggestion.StationID = StationID;
            Suggestion.AssociateID = ARep.GetAssociateByUserIDAndStationId(m_iUserID, StationID).ID;
            Suggestion.VideoID = Vrep.InsertReturnID(mod.video);
            Suggestion.Name = mod.video.Name;
            SRep.Add(Suggestion);
            SRep.Save();
            return RedirectToAction("Suggest", id);
        }

        public ActionResult Insert(int suggestionid, int scheduleid)
        //Samþykkir tillögur frá aðilum
        {
            ///////// Authentication hluti //////////////////////////////
            SuggestionRepository SugRep = new SuggestionRepository();
            EpisodeRepository ERep = new EpisodeRepository();
            ScheduleRepository SchRep = new ScheduleRepository();
            StationRepository SRep = new StationRepository();
            Schedule mySchedule = SchRep.GetSchedule(scheduleid);
            Station myStation = SRep.GetStation(mySchedule.StationID);
            if (m_iUserID != myStation.OwnerID) return View("BadAccess");                
            /////////////////////////////////////////////////////////////
            Episode Ep = new Episode();
            try
            {
                Ep.OrderNum = ERep.GetNextOrderNum(Ep.ScheduleID);
            }
            catch (System.InvalidOperationException)
            {
                Ep.OrderNum = 1;
            }
            Ep.ScheduleID = scheduleid;
            Ep.VideoID = SugRep.GetSuggestion(suggestionid).VideoID;
            ERep.Add(Ep);
            ERep.Save();
            SugRep.Delete(SugRep.GetSuggestion(suggestionid));
            SugRep.Save();
            return RedirectToAction("Edit", new { id = scheduleid });
        }
        
    }
}
